package com.kingdee.eas.jky.helper;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.framework.ObjectBaseInfo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.LinkedHashSet;
import java.util.StringTokenizer;
public class StringUtil {
	public StringUtil()
    {
    }

    public static boolean isNumber(String numStr)
    {
        if(numStr == null)
            return false;
        double d;
        try
        {
            d = Double.parseDouble(numStr);
        }
        catch(NumberFormatException nex)
        {
            return false;
        }
        return true;
    }

    public static boolean isEmpty(String str)
    {
        if(str == null)
            return true;
        return str.trim().length() <= 0;
    }

    public static String toString(BigDecimal dec)
    {
        Method method = null;
        Class cls = dec.getClass();
        try
        {
            method = cls.getMethod("toPlainString", null);
        }
        catch(SecurityException e) { }
        catch(NoSuchMethodException e) { }
        if(method != null)
            try
            {
                return (String)method.invoke(dec, null);
            }
            catch(IllegalArgumentException e) { }
            catch(IllegalAccessException e) { }
            catch(InvocationTargetException e) { }
        return dec.toString();
    }

    public static String nameFromSplits(String splits[], int length)
    {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++)
        {
            if(i > 0)
                sb.append(".");
            sb.append(splits[i]);
        }

        return sb.toString();
    }

    public static String getKeyIdList(Object ids[])
    {
        if(ids == null || ids.length == 0)
            return null;
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < ids.length; i++)
        {
            if(sb.length() > 0)
                sb.append(",");
            sb.append("'");
            sb.append(ids[i]);
            sb.append("'");
        }

        return sb.toString();
    }

    public static String getKeyIdList(ObjectBaseInfo vos[], String delim)
    {
        if(vos == null || vos.length == 0)
            return null;
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < vos.length; i++)
        {
            if(vos[i] == null)
                continue;
            if(sb.length() > 0)
                sb.append(delim);
            sb.append(vos[i].getId().toString());
        }

        return sb.toString();
    }

    public static String getKeyIdListWithOutQuotationMask(Object ids[])
    {
        if(ids == null || ids.length == 0)
            return null;
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < ids.length; i++)
        {
            if(sb.length() > 0)
                sb.append(",");
            sb.append(ids[i]);
        }

        return sb.toString();
    }

    public static LinkedHashSet getKeyIdList(String ids, String delim)
    {
        StringTokenizer stk = new StringTokenizer(ids, delim);
        LinkedHashSet result = new LinkedHashSet();
        for(; stk.hasMoreTokens(); result.add(String.valueOf(stk.nextToken()).trim()));
        return result;
    }

    public static LinkedHashSet getKeyIdList(String ids)
    {
        return getKeyIdList(ids, ";");
    }

    public static String getFormatString(int precision)
    {
        if(precision == 0)
            return "%R-{#,##0}f";
        StringBuffer buffer = new StringBuffer();
        if(precision == 0)
            buffer.append("#");
        else
            buffer.append("0.");
        for(int i = 0; i < precision; i++)
            buffer = buffer.append("0");

        StringBuffer formatString = new StringBuffer();
        formatString.append("%r-[=]{#,##").append(buffer).append("}f");
        return formatString.toString();
    }


}
